use std::error::Error;
use std::fmt::{Display, Formatter};

/// 解析错误
#[derive(Debug)]
pub enum ParseErr {
    Malformed, // 变形的，畸形的 ==> 格式不正确
    Empty,     // 文件为空
}

/// 读取错误
#[derive(Debug)]
pub struct ReadErr {
    pub child_err: Box<dyn Error>,
}

// Error特征要求的
impl Display for ReadErr {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "Failed reading todo file")
    }
}

impl Display for ParseErr {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "Todo list parsing failed")
    }
}

// 实现Error特征
impl Error for ReadErr {
    fn description(&self) -> &str {
        "Todolist read failed"
    }

    fn cause(&self) -> Option<&dyn Error> {
        Some(&*self.child_err)
    }
}

impl Error for ParseErr {
    fn description(&self) -> &str {
        "Todolist parse failed"
    }

    fn cause(&self) -> Option<&dyn Error> {
        None
    }
}